﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;

namespace GUI
{
    public partial class TraCuuChuyenBay_GUI : DevExpress.XtraEditors.XtraUserControl
    {
        public TraCuuChuyenBay_GUI()
        {
            InitializeComponent();
        }
        BindingList<TraCuuChuyenBay_DTO> list = new BindingList<TraCuuChuyenBay_DTO>();
        List<SanBay_DTO> listSanBayDi = SanBay_BUS.GetSanBayList();
        List<SanBay_DTO> listSanBayDen = SanBay_BUS.GetSanBayList();
        private void TraCuuChuyenBay_GUI_Load(object sender, EventArgs e)
        {
            cmbSanBayDi.ValueMember = SanBayColumns.MaSanBay;
            cmbSanBayDi.DisplayMember = SanBayColumns.TenSanBay;
            cmbSanBayDi.DataSource = listSanBayDi;

            SanBayDi.ValueMember = SanBayColumns.MaSanBay;
            SanBayDi.DisplayMember = SanBayColumns.TenSanBay;
            SanBayDi.DataSource = listSanBayDi;

            cmbSanBayDen.ValueMember = SanBayColumns.MaSanBay;
            cmbSanBayDen.DisplayMember = SanBayColumns.TenSanBay;
            cmbSanBayDen.DataSource = listSanBayDen;

            SanBayDen.ValueMember = SanBayColumns.MaSanBay;
            SanBayDen.DisplayMember = SanBayColumns.TenSanBay;
            SanBayDen.DataSource = listSanBayDen;
        }


        bool KiemTraNgay()
        {
            if (dateKetThuc.Value <= dateBatDau.Value)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        bool IsNumber(string str)
        {
            if (str == null)
            {
                return false;
            }
            foreach (Char item in str)
            {
                if (!Char.IsNumber(item))
                {
                    return false;
                }
            }
            return true;
        }
        bool KiemTraSoVe()
        {
            if (int.Parse(txtVe2.Text) < int.Parse(txtVe1.Text))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdTuyenBay.Checked == true)
            {
                string SanBayDi = cmbSanBayDi.SelectedValue.ToString();
                string SanBayDen = cmbSanBayDen.SelectedValue.ToString();
                gridC_DSChuyenBay.DataSource = null;
                list = TraCuuChuyenBay_BUS.GetChuyenBayTheoTuyenBayList(SanBayDi, SanBayDen);
                if (list.Count > 0)
                {
                    gridC_DSChuyenBay.DataSource = list;
                }
                else
                {
                    MessageBox.Show("Không Có Chuyến Bay Nào", "..:: Thông Báo ::..");
                }
            }
            if (rdNgayBay.Checked == true)
            {
                if (KiemTraNgay() == true)
                {
                    DateTime Tu = dateBatDau.Value;
                    DateTime Den = dateKetThuc.Value;
                    gridC_DSChuyenBay.DataSource = null;
                    list = TraCuuChuyenBay_BUS.GetChuyenBayTheoNgayBayList(Tu, Den);
                    if (list.Count > 0)
                    {
                        gridC_DSChuyenBay.DataSource = list;
                    }
                    else
                    {
                        MessageBox.Show("Không Có Chuyến Bay Nào", "..:: Thông Báo ::..");
                    }
                }
                else
                {
                    MessageBox.Show("Vui Lòng Chọn Thông Tin", "..:: Thông Báo ::..");
                }
            }
            if (rdVe.Checked == true)
            {
                if (txtVe1.Text != "" && txtVe2.Text != "")
                {
                    if ((IsNumber(txtVe1.Text) == true) && (IsNumber(txtVe2.Text) == true) && (KiemTraSoVe() == true))
                    {
                        int Tu = int.Parse(txtVe1.Text);
                        int Den = int.Parse(txtVe2.Text);
                        gridC_DSChuyenBay.DataSource = null;
                        list = TraCuuChuyenBay_BUS.GetChuyenBayTheoSoGheTrongList(Tu, Den);
                        if (list.Count > 0)
                        {
                            gridC_DSChuyenBay.DataSource = list;
                        }
                        else
                        {
                            MessageBox.Show("Không Có Chuyến Bay Nào", "..:: Thông Báo ::..");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Vui Lòng Nhập Lại Thông Tin", "..:: Thông Báo ::..");
                    }
                }
                else
                {
                    MessageBox.Show("Vui Lòng Nhập Thông Tin", "..:: Thông Báo ::..");
                }
            }
        }

        private void rdTuyenBay_CheckedChanged(object sender, EventArgs e)
        {
            cmbSanBayDi.Enabled = true;
            cmbSanBayDen.Enabled = true;
            dateBatDau.Enabled = false;
            dateKetThuc.Enabled = false;
            txtVe1.Enabled = false;
            txtVe2.Enabled = false;
            txtVe1.Text = "";
            txtVe2.Text = "";
        }

        private void rdNgayBay_CheckedChanged(object sender, EventArgs e)
        {
            dateBatDau.Enabled = true;
            dateKetThuc.Enabled = true;
            cmbSanBayDi.Enabled = false;
            cmbSanBayDen.Enabled = false;
            txtVe1.Enabled = false;
            txtVe2.Enabled = false;
            txtVe1.Text = "";
            txtVe2.Text = "";
        }

        private void rdVe_CheckedChanged(object sender, EventArgs e)
        {
            txtVe1.Enabled = true;
            txtVe2.Enabled = true;
            dateBatDau.Enabled = false;
            dateKetThuc.Enabled = false;
            cmbSanBayDi.Enabled = false;
            cmbSanBayDen.Enabled = false;
        }
    }
}
